package com.zhuannimei;



import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.xuxihong1.Dianle;
import com.xxhong.znm.AdManager;
import com.xxhong.znm.offers.OffersManager;
import com.zhuannimei.R;
import com.zhuannimei.fragment.Fragment1;
import com.zhuannimei.fragment.Fragment1.Callbacks;
import com.zhuannimei.fragment.Fragment2;
import com.zhuannimei.fragment.Fragment3;
import com.zhuannimei.fragment.Fragment4;
import com.zhuannimei.ui.BaseActivity;
import com.zhuannimei.ui.LaunchActivity;
import com.zhuannimei.utility.RSA;
import com.zhuannimei.utility.UiUtil;
import com.zhuannimei.utility.UserData;
import com.zhuannimei.utility.WuLog;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;

public class MainActivity extends BaseActivity implements Callbacks{
	
	private static final String TAG = "MainActivity";

	// 定义FragmentTabHost对象
	private FragmentTabHost mTabHost;
	private RadioGroup mTabRg;
	private final Class[] fragments = { Fragment1.class, Fragment2.class,
			Fragment3.class, Fragment4.class };
	private FragmentManager fManager;
	private String userUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		fManager = getSupportFragmentManager();
		initView();
		initAd();
		userUserId = UserData.getInstance().getUserUserId();
		WuLog.i(TAG, "用户id"+userUserId);
		if(!TextUtils.isEmpty(userUserId)){
			setAliasToJpush();
		}
	}
	
	private void setAliasToJpush(){
		JPushInterface.setAliasAndTags(MainActivity.this, userId, null, new TagAliasCallback(
				) {
			@Override
			public void gotResult(int arg0, String arg1, Set<String> arg2) {
				WuLog.i("xxhong", arg0+"");
				if(arg0!=0){
					SystemClock.sleep(200);
					WuLog.i(TAG, "注册别名到极光服务器:"+arg0);
					setAliasToJpush();
				}
			}
		});
	}

	public void initAd() {
		Dianle.initDianleContext(this, "e6b3cc860c3f54a7241c5fa65d3adff6");
		Dianle.setCustomActivity("com.zhuannimei.activity.MyView");
		Dianle.setCustomService("com.zhuannimei.activity.MyService");
		//test --- 测试代码
		AdManager.getInstance(this).init("473a6cc44a32b2dc",
				"287385a131da7356", false);
		// 如果使用积分广告，请务必调用积分广告的初始化接口:
		OffersManager.getInstance(this).onAppLaunch();
	
	}

	private void checkIsLogin() {
		Intent intent = new Intent(this,LaunchActivity.class);
		startActivity(intent);
	}

	private void initView() {
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		// 得到fragment的个数
		int count = fragments.length;
		for (int i = 0; i < count; i++) {
			// 为每一个Tab按钮设置图标、文字和内容
			TabSpec tabSpec = mTabHost.newTabSpec(i + "").setIndicator(i + "");
			// 将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, fragments[i], null);
		}

		mTabRg = (RadioGroup) findViewById(R.id.tab_rg_menu);
		mTabRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.tab_rb_1:
					mTabHost.setCurrentTab(0);
					break;
				case R.id.tab_rb_2:
					mTabHost.setCurrentTab(1);

					break;
				case R.id.tab_rb_3:

					mTabHost.setCurrentTab(2);
					break;
				case R.id.tab_rb_4:

					mTabHost.setCurrentTab(3);
					break;

				default:
					break;
				}
			}
		});

		mTabHost.setCurrentTab(0);
	}

	@Override
	public void onFragmentMethodCallBack(String id) {
		Dianle.showOffers();
	}


}
